﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 通用数据库访问类
    /// </summary>
    public class DBHelper
    {
        //连接配置文件的数据库参数生成数据库连接字符串
        private static string str = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
        //建立数据库连接对象
        public static SqlConnection conn = new SqlConnection(str);

        // 执行数据库查询，返回意向数据结果
        public static object GetScalar(SqlCommand cmd)
        {
            cmd.Connection = conn; //建立数据库对象连接
            object ob = new SqlCommand();
            try
            {
                if(conn.State== ConnectionState.Closed)
                {
                    conn.Open();//打开数据库连接
                }
                ob = cmd.ExecuteScalar();//调用SqlCommand对象的ExcuteScalar方法访问数据库
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return ob;
        }
    }
}
